﻿package edu.pxxy.yl;

import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import edu.pxxy.yl.adapt.MenuAdapt;
import edu.pxxy.yl.dao.FoodDao;
import edu.pxxy.yl.dao.FoodDaoImpl;
import edu.pxxy.yl.db.MyDBOpenHelper;
import edu.pxxy.yl.domain.Food;


public class MainActivity extends AppCompatActivity {
    //GITEE:https://gitee.com/sugars_yuli/android_final_assignment.git
    private FoodDao foodDao;
    private ListView foodViewList;
    private Button menu;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData(this.getApplicationContext());
        initView();
        foodDao= FoodDaoImpl.getInstence(this.getApplicationContext());
        List<Food> foodList=foodDao.queryFoodByFood(null);
        System.out.println(foodList);
        MenuAdapt menuAdapt=new MenuAdapt(MainActivity.this,R.layout.item,foodList);
        foodViewList.setAdapter(menuAdapt);
        bindEvent();
    }

    private void bindEvent() {
        menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,MenuManageActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        foodViewList=findViewById(R.id.foodList);
        menu=findViewById(R.id.menu);
    }

    private void initData(Context context) {
        SQLiteDatabase sqLiteDatabase=new MyDBOpenHelper(context).getReadableDatabase();
        sqLiteDatabase.beginTransaction();
        try{
            sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count)values('小炒肉',20,0)");
            sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count)values('青椒肉丝',24,0)");
            sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count)values('西红柿炒鸡蛋',12,0)");
            sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count)values('红烧鱼头',8,0)");
            sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count)values('红烧猪蹄',25,0)");
            sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count)values('红烧鱼块',18,0)");
            sqLiteDatabase.execSQL("insert into food(foodname,foodprice,count)values('紫菜蛋汤',10,0)");
            sqLiteDatabase.setTransactionSuccessful();
        }finally {
            sqLiteDatabase.endTransaction();
        }
    }
}
